package com.itheima.sh.c;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

@WebServlet("/ajaxJsonDemo3Servlet")
public class AjaxJsonDemo3Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        User user = JSON.parseObject(request.getInputStream(), User.class);
        System.out.println(user);

        ArrayList<User> list = new ArrayList<>();
        Collections.addAll(list,new User("张三","1234"),
                        new User("柳岩","1234"),
                        new User("李四","1234"));
        String s = JSON.toJSONString(list);
        response.getWriter().println(s);
    }
}